package leetcode.editor.cn;

import leetcode.editor.cn.common.ListNode;

/**
 * @Author: Dempsey
 * @Data: 2021-03-21 20:54:53
 */

//给定一个排序链表，删除所有含有重复数字的节点，只保留原始链表中 没有重复出现 的数字。 
//
// 示例 1: 
//
// 输入: 1->2->3->3->4->4->5
//输出: 1->2->5
// 
//
// 示例 2: 
//
// 输入: 1->1->1->2->3
//输出: 2->3 
// Related Topics 链表 
// 👍 480 👎 0


public class P82 {
    public static void main(String[] args) {
        Solution solution = new P82().new Solution();
    }

//leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            ListNode dummy = new ListNode();
            dummy.next = head;

            ListNode prev = dummy;
            ListNode cur = prev.next;
            while (cur != null) {
                int curRepeatNum = 0;
                ListNode difNode = cur;
                // 找到和cur指向的结点值不同的结点
                while (difNode != null && difNode.val == cur.val) {
                    curRepeatNum++;
                    difNode = difNode.next;
                }

                // 如果curRepeatNum的值大于1，则表示cur指向的结点重复出现了
                if (curRepeatNum > 1) {
                    prev.next = difNode;
                } else {
                    // cur指向的结点没有重复出现，则将变量prev指向cur所指向的结点
                    prev = cur;
                }
                cur = difNode;
            }

            return dummy.next;

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}